﻿using System;
using System.Collections.Generic;

using Storyteller.Framework.Graphics;
using Storyteller.Framework.Math;

namespace Storyteller.Framework
{
	public static class ExtensionMethods
	{
		public static bool NextBool(this Random random)
		{
			return random.Next(0, 2) == 1;
		}

		public static float NextFloat(this Random random)
		{
			return (float)random.NextDouble();
		}

		public static float NextFloat(this Random random, float minimum, float maximum)
		{
			return MathHelper.Lerp(minimum, maximum, random.NextFloat());
		}

		public static Color NextColor(this Random random, Color minimum, Color maximum)
		{
			Color c = new Color(
				MathHelper.Lerp(minimum.R, maximum.R, random.NextFloat()),
				MathHelper.Lerp(minimum.G, maximum.G, random.NextFloat()),
				MathHelper.Lerp(minimum.B, maximum.B, random.NextFloat()),
				MathHelper.Lerp(minimum.A, maximum.A, random.NextFloat()));

			return c;
		}

		public static Vector2 NextVector2(this Random random, Vector2 minimum, Vector2 maximum)
		{
			return new Vector2(MathHelper.Lerp(minimum.X, maximum.X, random.NextFloat()),
				MathHelper.Lerp(minimum.Y, maximum.Y, random.NextFloat()));
		}

		public static Vector2 NextUnitVector2(this Random random)
		{
			float radians = random.NextFloat(-3.141593f, 3.141593f);

			return new Vector2((float)System.Math.Cos(radians), (float)System.Math.Sin(radians));
		}

#if XBOX
		public static int RemoveAll<T>(this List<T> list, Predicate<T> match)
		{
			if (list == null)
				throw new NullReferenceException();

			if (match == null)
				throw new ArgumentNullException("match");

			int i = 0;
			int j = 0;

			for (i = 0; i < list.Count; i++)
			{
				if (!match(list[i]))
				{
					if (i != j)
						list[j] = list[i];

					j++;
				}
			}

			int removed = i - j;
			if (removed > 0)
				list.RemoveRange(list.Count - removed, removed);

			return removed;
		}
#endif
	}
}
